package com.sky.web.app;


import com.sky.context.UserHolder;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.Orders;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Slf4j
public class AppOrderController {


    @Autowired
    private OrderService orderService;



    //用户下单(点击去支付)
    @PostMapping("/submit")
    public Result save(@RequestBody Orders orders) {//1.接收请求参数
        //2.调用Service
        OrderSubmitVO vo =orderService.save(orders);
        //3.返回结果
        return Result.success(vo);
    }

    // 订单支付
    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    //历史订单查询
    @GetMapping("/historyOrders")
    public Result historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        //1.填充登录人id
        ordersPageQueryDTO.setUserId(UserHolder.get());
        log.info("历史订单查询：{}", ordersPageQueryDTO);
        PageBean<OrderVO> pageBean =orderService.findByPage(ordersPageQueryDTO);
        return Result.success(pageBean);
    }

    //查询订单详情
    @GetMapping("/orderDetail/{id}")
    public Result findById(@PathVariable Long id){
        OrderVO orderVO = orderService.findById(id);
        return Result.success(orderVO);
    }
    //取消订单
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        orderService.cancelById(id);
        return Result.success();
    }
    //再来一单
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id) {
        orderService.repetitionById(id);
        return Result.success();
    }

    //催单
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        orderService.reminderById(id);
        return Result.success();
    }
}
